<?php
/**
* @file $Id: Periods.php 580 2007-06-05 19:19:11Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

if($_REQUEST['values'] && $_POST['values'] && AllowEdit())
{
	foreach($_REQUEST['values'] as $id=>$columns)
	{
		if($columns['START_HOUR'])
		{	
			$columns['START_TIME'] = $columns['START_HOUR'].':'.$columns['START_MINUTE'].' '.$columns['START_M'];
			unset($columns['START_HOUR']);unset($columns['START_MINUTE']);unset($columns['START_M']);
		}
		if($columns['END_HOUR'])
		{
			$columns['END_TIME'] = $columns['END_HOUR'].':'.$columns['END_MINUTE'].' '.$columns['END_M'];
			unset($columns['END_HOUR']);unset($columns['END_MINUTE']);unset($columns['END_M']);
		}
		
		if($id!='new')
		{
			$sql = "UPDATE SCHOOL_PERIODS SET ";
							
			foreach($columns as $column=>$value)
			{
				$sql .= $column."='".str_replace("\'","''",$value)."',";
			}
			$sql = substr($sql,0,-1) . " WHERE PERIOD_ID='$id'";
			DBQuery($sql);
		}
		else
		{
			$sql = "INSERT INTO SCHOOL_PERIODS ";

			$fields = 'PERIOD_ID,SCHOOL_ID,SYEAR,';
			$values = db_seq_nextval('SCHOOL_PERIODS_SEQ').",'".UserSchool()."','".UserSyear()."',";

			$go = 0;
			foreach($columns as $column=>$value)
			{
				if($value)
				{
					$fields .= $column.',';
					$values .= "'".str_replace("\'","''",$value)."',";
					$go = true;
				}
			}
			$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
			
			if($go)
				DBQuery($sql);
		}
	}
}

DrawHeader(ProgramTitle());

if($_REQUEST['modfunc']=='remove' && AllowEdit())
{
	if(DeletePrompt(_('period')))
	{
		DBQuery("DELETE FROM SCHOOL_PERIODS WHERE PERIOD_ID='$_REQUEST[id]'");
		unset($_REQUEST['modfunc']);
	}
}

if($_REQUEST['modfunc']!='remove')
{
	$sql = "SELECT PERIOD_ID,TITLE,SHORT_NAME,SORT_ORDER,LENGTH,START_TIME,END_TIME,BLOCK,ATTENDANCE,
			LENGTH_U,LENGTH_M,LENGTH_T,LENGTH_W,LENGTH_H,LENGTH_F,LENGTH_S,LENGTH_S
			FROM SCHOOL_PERIODS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' ORDER BY SORT_ORDER";
	$functions = array('TITLE'=>'_makeTextInput','SHORT_NAME'=>'_makeTextInput','SORT_ORDER'=>'_makeTextInput','BLOCK'=>'_makeTextInput','LENGTH'=>'_makeTextInput','START_TIME'=>'_makeTimeInput','END_TIME'=>'_makeTimeInput','ATTENDANCE'=>'_makeCheckboxInput');
	if(SystemPreferences('VARIABLE_PERIOD_LENGTH')=='Y')
		$functions['LENGTH'] = '_makeLength';
	$periods_RET = DBGet(DBQuery($sql),$functions);

	$columns = array('TITLE'=>_('Title'),'SHORT_NAME'=>_('Short Name'),'SORT_ORDER'=>_('Sort Order'),'LENGTH'=>_('Length (minutes)'),'BLOCK'=>_('Block'),'ATTENDANCE'=>_('Used for Attendance'));
	//,'START_TIME'=>'Start Time','END_TIME'=>'End Time'
	$link['add']['html'] = array('TITLE'=>_makeTextInput('','TITLE'),'SHORT_NAME'=>_makeTextInput('','SHORT_NAME'),'LENGTH'=>_makeTextInput('','LENGTH'),'SORT_ORDER'=>_makeTextInput('','SORT_ORDER'),'BLOCK'=>_makeTextInput('','BLOCK'),'START_TIME'=>_makeTimeInput('','START_TIME'),'END_TIME'=>_makeTimeInput('','END_TIME'),'ATTENDANCE'=>_makeCheckboxInput('','ATTENDANCE'));
	if(SystemPreferences('VARIABLE_PERIOD_LENGTH')=='Y')
		$link['add']['html']['LENGTH'] = _makeLength('','LENGTH');

	$link['remove']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=remove";
	$link['remove']['variables'] = array('id'=>'PERIOD_ID');
	
	echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&modfunc=update method=POST>";
	DrawHeader('',SubmitButton(_('Save')));
	ListOutput($periods_RET,$columns,_('Period'),_('Periods'),$link);
	echo '<CENTER>'.SubmitButton(_('Save')).'</CENTER>';
	echo '</FORM>';
}

function _makeTextInput($value,$name)
{	global $THIS_RET;
	
	if($THIS_RET['PERIOD_ID'])
	{
		$id = $THIS_RET['PERIOD_ID'];
		$div = 'force';
	}
	else
	{
		$id = 'new';
		$div = false;
	}

	if($name!='TITLE')
		$extra = 'size=5 maxlength=10';
	
	return TextInput($value,'values['.$id.']['.$name.']','',$extra,$div);
}

function _makeCheckboxInput($value,$name)
{	global $THIS_RET;
	
	if($THIS_RET['PERIOD_ID'])
		$id = $THIS_RET['PERIOD_ID'];
	else
		$id = 'new';
	
	return CheckboxInput($value,'values['.$id.']['.$name.']','','',($id=='new'?true:false),'<IMG SRC=assets/check.gif height=15>','<IMG SRC=assets/x.gif height=15>');
}

function _makeTimeInput($value,$name)
{	global $THIS_RET;

	if($THIS_RET['PERIOD_ID'])
		$id = $THIS_RET['PERIOD_ID'];
	else
		$id = 'new';

	$hour = substr($value,0,strpos($value,':'));
	$minute = substr($value,strpos($value,':'),strpos($value,' '));
	$m = substr($value,strpos($value,' '));

	for($i=1;$i<=11;$i++)
		$hour_options[$i] = $i;
	$hour_options['0'] = '12';
	
	for($i=0;$i<=9;$i++)
		$minute_options[$i] = '0'.$i;
	for($i=10;$i<=59;$i++)
		$minute_options[$i] = $i;
	
	$m_options = array('AM'=>'AM','PM'=>'PM');

	if($id!='new' && $value)
		return '<DIV id=time'.$id.'><div onclick=\'addHTML("<TABLE><TR><TD>'.str_replace('"','\"',SelectInput($hour,'values['.$id.'][START_HOUR]','',$hour_options,false,'',false)).':</TD><TD>'.str_replace('"','\"',SelectInput($minute,'values['.$id.'][START_MINUTE]','',$minute_options,false,'',false)).'</TD><TD>'.str_replace('"','\"',SelectInput($m,'values['.$id.'][START_M]','',$m_options,false,'',false)).'</TD></TR></TABLE>","time'.$id.'",true);\'>'.$value.'</div></DIV>';
	else
		return '<TABLE><TR><TD>'.SelectInput($hour,'values['.$id.'][START_HOUR]','',$hour_options,false,'',false).':</TD><TD>'.SelectInput($minute,'values['.$id.'][START_MINUTE]','',$minute_options,false,'',false).'</TD><TD>'.SelectInput($m,'values['.$id.'][START_M]','',$m_options,false,'',false).'</TD></TR></TABLE>';
}

function _makeLength($na,$name)
{	global $THIS_RET;

	if($THIS_RET['PERIOD_ID'])
	{
		$id = $THIS_RET['PERIOD_ID'];
		$div = 'force';
	}
	else
	{
		$id = 'new';
		$div = false;
	}

	$header .= '<TABLE><TR>';
	$days = array('U','M','T','W','H','F','S');
	foreach($days as $day)
	{
		$value = $THIS_RET['LENGTH_'.$day];

		$header .= '<TD>'.TextInput($value,'values['.$id.'][LENGTH_'.$day.']',($day=='U'?'S':$day),'size=2 maxlength=3',$div).'</TD>';
	}
	$header .= '</TR></TABLE>';

	return $header;
}

?>